package jp.sourceforge.akjrcp.image.clipboard;

import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.IOException;

import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Display;

public class ImageClipboardUtil {
	
//	setContents
	 public static void setClipboard(Display display,String text){
		 Clipboard cp= new Clipboard(display);
		 cp.setContents(new Object[]{text},
                new Transfer[]{TextTransfer.getInstance()});
	 }
   public static String getCurrentText(Display display){
       String result=null;
       Clipboard cp= new Clipboard(display);
       TextTransfer transfer=TextTransfer.getInstance();
       
       String v[]=cp.getAvailableTypeNames();
       
       for(int i=0;i<v.length;i++){
          if(v[i].equals("CF_TEXT")){
              result= (String)cp.getContents(transfer);
          }
       }
       v=null;
       transfer=null;
       cp.dispose();
       return result;
   }
   
   
	   public static BufferedImage getClipboardImage() {
	        Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
	    
	        try {
	            if (t != null && t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
	            	BufferedImage image = (BufferedImage)t.getTransferData(DataFlavor.imageFlavor);
	                return image;
	            }
	        } catch (UnsupportedFlavorException e) {
	        } catch (IOException e) {
	        }
	        return null;
	    }
	   
		public static boolean isSameImage(BufferedImage image1, BufferedImage image2) {
			if (image1 == null || image2 == null) {
				return false;
			}
			Raster raster1 = image1.getRaster();
			Raster raster2 = image2.getRaster();
			
			return isSameImage(raster1,raster2);
		}
		
		public static boolean isSameImage(Raster raster1, Raster raster2) {
			if (raster1 == null || raster2 == null) {
				return false;
			}
			
			int bf1[] = raster1.getPixels(0, 0, raster1.getWidth(), raster1
					.getHeight(), (int[]) null);
			int bf2[] = raster2.getPixels(0, 0, raster2.getWidth(), raster2
					.getHeight(), (int[]) null);
			if (bf1.length != bf2.length) {
				return false;
			}
			for (int i = 0; i < bf1.length; i++) {
				if (bf1[i] != bf2[i]) {
					return false;
				}
			}
			return true;
		}
		
		public static boolean isSameImage(int[] pixels1, int[] pixels2) {
			if (pixels1 == null || pixels2 == null) {
				return false;
			}
			
			if (pixels1.length != pixels2.length) {
				return false;
			}
			for (int i = 0; i < pixels1.length; i++) {
				if (pixels1[i] != pixels2[i]) {
					return false;
				}
			}
			return true;
		}
}
